﻿/******************************************************************
** 文件名:T_003Novel_TypesDAL.cs
** Copyright (c) 
** 创建人:
** 日 期:2012-11-15 11:25:38
** 描 述:数据操作类
** 版 本:v1.0
********************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using Novel.Entity;
using Novel.DAL.Base;

namespace Novel.DAL
{
    /// <summary>
    /// 数据操作类：T_003Novel_Types_DAL
    /// </summary>
    public class T_003Novel_Types_DAL : IT_003Novel_TypesManager
    {


        #region 表名
        public  string GetTableName()
        {
            return "T_003Novel_Types";
        }
        #endregion
        #region 插入记录
        /// <summary>
        /// 插入记录
        /// </summary>
        /// <returns></returns>
        public int Insert(T_003Novel_TypesEntity model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.AppendFormat("INSERT INTO {0}",GetTableName());
			strSql.Append("(Type_Name,ParentID,Status)");
			strSql.Append(" values ");
			strSql.Append("(@Type_Name,@ParentID,@Status)");
            strSql.Append(" SELECT @Type_ID = @@IDENTITY");
			SqlCommand cmd = new SqlCommand(strSql.ToString());
			cmd.Parameters.Add("@Type_ID",SqlDbType.Int);
			cmd.Parameters["@Type_ID"].Direction = ParameterDirection.Output;
			cmd.Parameters.Add("@Type_Name",SqlDbType.NVarChar);
			cmd.Parameters["@Type_Name"].Value = model.Type_Name;
			cmd.Parameters.Add("@ParentID",SqlDbType.Int,4);
			cmd.Parameters["@ParentID"].Value = model.ParentID;
			cmd.Parameters.Add("@Status",SqlDbType.TinyInt,1);
			cmd.Parameters["@Status"].Value = model.Status;
		
			SQLHelper da = new SQLHelper();
			int EffectRows = da.ExecuteNonQuery(cmd);
            model.Type_ID = (int)cmd.Parameters["@Type_ID"].Value;
			return EffectRows;
		}
        #endregion

        #region 更新数据
        /// <summary>
        /// 更新数据
        /// </summary>
        /// <returns></returns>
        public int Update(T_003Novel_TypesEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("UPDATE {0} SET ", GetTableName());
            strSql.Append("Type_Name=@Type_Name,ParentID=@ParentID,Status=@Status");
            strSql.Append(" WHERE Type_ID=@Type_ID");
            SqlCommand cmd = new SqlCommand(strSql.ToString());
            cmd.Parameters.Add("@Type_ID", SqlDbType.Int, 4);
            cmd.Parameters["@Type_ID"].Value = model.Type_ID;
            cmd.Parameters.Add("@Type_Name", SqlDbType.NVarChar);
            cmd.Parameters["@Type_Name"].Value = model.Type_Name;
            cmd.Parameters.Add("@ParentID", SqlDbType.Int, 4);
            cmd.Parameters["@ParentID"].Value = model.ParentID;
            cmd.Parameters.Add("@Status", SqlDbType.TinyInt, 1);
            cmd.Parameters["@Status"].Value = model.Status;
          
            SQLHelper da = new SQLHelper();
            return da.ExecuteNonQuery(cmd);
        }
        #endregion

        #region 删除数据
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <returns></returns>
        public int Delete(int id)
        {
            string sql = string.Format("DELETE {0} WHERE Type_ID=@Type_ID", GetTableName());
            SqlCommand command = new SqlCommand(sql);
            command.Parameters.Add("@Type_ID", SqlDbType.Int, 4);
            command.Parameters["@Type_ID"].Value = id;
            SQLHelper da = new SQLHelper();
            return da.ExecuteNonQuery(command);
        }
        #endregion

        #region IEntityFacade 成员
        /// <summary>
        /// 填充数据
        /// </summary>
        /// <param name="reader"></param>
        public EntityBase FillDetailWithReader(IDataReader reader)
        {
            T_003Novel_TypesEntity model = new T_003Novel_TypesEntity();
            if (reader["Type_ID"] != DBNull.Value)
                model.Type_ID = (int)reader["Type_ID"];
            if (reader["Type_Name"] != DBNull.Value)
                model.Type_Name = (string)reader["Type_Name"];
            if (reader["ParentID"] != DBNull.Value)
                model.ParentID = (int)reader["ParentID"];
            if (reader["Status"] != DBNull.Value)
                model.Status = (T_003Novel_TypesEntity.EnumStatus)(byte)reader["Status"];
            if (reader["AddTime"] != DBNull.Value)
                model.AddTime = (DateTime)reader["AddTime"];
            return model;
        }
        #endregion

        public T_003Novel_TypesEntity GetObjectByID(int ID)
        {
            SQLHelper da = new SQLHelper();
            string strSql = string.Format("select * {0} WHERE BookID=@BookID", GetTableName());
            SQLHelper.MappEntity<T_003Novel_TypesEntity> dele_Entity = delegate(SqlDataReader reader)
            {
                return (T_003Novel_TypesEntity)FillDetailWithReader(reader);
            };
            return da.Exceute<T_003Novel_TypesEntity>(new SqlCommand(strSql), dele_Entity);

        }


        #region 数据集
        public DataTable ShowTable()
        {

            string strSql = string.Format("SELECT  * FROM {0}", GetTableName());
            return SQLHelper.Execute(strSql.ToString());
        }
        #endregion

        #region 分页数据集
        public DataTable ShowTable(PageEntity entity, out int totalPages, out int totalCount)
        {
            QueryBase manager = new QueryBase();

            entity.tbName = GetTableName();
            DataTable dt = manager.GetTable(entity);
            totalPages = entity.TotalPages;
            totalCount = entity.TotalCount;
            return dt;
        }
        #endregion
        /// <summary>
        /// 通过子类名称获取ID、章节表名
        /// </summary>
        /// <returns></returns>
        public void GetIDAndTableNameByName(string SubTypeName, out int mainID, out int subID, out string chapterTableName)
        {
            mainID = 0;
            subID = 0;
            chapterTableName = "";
            string strSql = string.Format("select t.type_ID,t.parentID,ChapterTableName from T_003Novel_Types t,T_007ChapterConfig c where t.parentID=c.NovelType and t.type_Name='{0}'  and c.status=1", SubTypeName);
           DataTable dt=  SQLHelper.Execute(strSql.ToString());
           if (dt != null && dt.Rows.Count > 0)
           {
               subID = int.Parse(dt.Rows[0]["type_ID"].ToString());
               mainID = int.Parse(dt.Rows[0]["parentID"].ToString());
               chapterTableName = dt.Rows[0]["ChapterTableName"].ToString();
           }

        }

    }
}
